// 从rgba中获取颜色值列表
export function getColorListFromRGBA(rgba: string) {
  if (!rgba) return;
  const rgbaList = rgba.match(/(\d(\.\d+)?)+/g);
  if (!rgbaList) {
    throw new Error('色值提取失败');
  }
  return rgbaList;
}

// rgba设置透明度
export function setColorAndAlpha(rgba: string, alpha: number): string {
  const alphaFloat = alpha / 100;
  const colorList = getColorListFromRGBA(rgba);
  const alphaRgx = /^rgba\((,?\s*\d+).+$/;
  return rgba.replace(
    alphaRgx,
    `rgba(${colorList?.[0]}, ${colorList?.[1]}, ${colorList?.[2]}, ${alphaFloat})`,
  );
}

// 格式化当前时间
export function formatDateTime() {
  const date = new Date();
  const year = date.getFullYear().toString().padStart(4, '0');
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  const hour = date.getHours().toString().padStart(2, '0');
  const minute = date.getMinutes().toString().padStart(2, '0');
  const second = date.getSeconds().toString().padStart(2, '0');
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
